// index/details.js
var app = getApp()

let api = require("../../utils/api.js")
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    // 轮播图
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    

    goodList:{     
      "code": 200,
     "msg": "成功",
     "data": {
        id:1,
         //# 是否收藏
     is_shoucang:false,
      //# 商品信息
     goods_info: { 
         goods_id: 1, goods_title: "商品标题1", goods_price: '100', goods_time: "2天", goods_diqu: "全国", goods_xiaoliang: 1000, content:'商品介绍详情商品介绍详情商品介绍详情商品介绍详情商品介绍详情商品介绍详情商品介绍详情'
     },
     //# 商品图片
     goods_img: [
         {'img': 'https://tgi12.jia.com/129/144/29144025.jpg'},
         {'img': 'https://tgi1.jia.com/129/144/29144031.jpg' },
         {'img': 'https://tgi1.jia.com/129/144/29144032.jpg' },
         {'img': 'https://tgi13.jia.com/127/342/27342599.jpg' },
         ],
     //评论数据 取前三条
     pjDataList: [
     { headpic: 'https://tgi13.jia.com/127/342/27342599.jpg', author: '张三', add_time: '2018-06-01', content:'好评好评，真实太好了!'},
     { headpic: 'https://tgi13.jia.com/127/342/27342599.jpg', author: '张三', add_time: '2018-06-01', content: '好评好评，真实太好了!' }
   ],//评价数据
     }
   }
  },
 
 // 预览轮播图
  previewImage: function (e) {
    let src=e.currentTarget.dataset.src;
    let url=e.currentTarget.dataset.url;
    // console.log(src,'>>src>')
    // console.log(e.currentTarget.dataset)
    // console.log(e.currentTarget.dataset.url)
    var imgList = []; 
    for (let i = 0; i < url.length; i++) {
      imgList.push(url[i].img);
    }
    wx.previewImage({
      current:src,
      urls: imgList,
    })
  },

 // 下单
 buy:function(e) {
  let goodId = e.target.dataset.id
  //  console.log(e.target.dataset.id)
  wx.navigateTo({
    url: '/pages/pay/pay?Id=' + goodId,
  })
 },

 // 获取详情页数据
 getDetail:function (goodId) {
  var token = wx.getStorageSync('token')
   wx.request({
     url: api.IndexDetailApi + goodId+'/',
     header: {'Authorization':'JWT'+' '+token},
     dataType: "json",
     method: "GET",
     success: (result) => {
      //  console.log(result.data.data,'>>>>>')
       this.setData({
        goodList:result.data
       })
      //  console.log(this.data.goodList,'>>>>>')
     },
   })
 },
 //收藏
 jiarushoucang(e){
  const id= e.currentTarget.dataset['id'];
  const is_shoucang= e.currentTarget.dataset['is_shoucang'];
  // console.log(id)    
  try {
    var token = wx.getStorageSync('token')   //从缓存中拿到token
    // console.log(token,'0000000')
    // var token =app.globalData.token
    // console.log(token[0])
    // console.log(token)
    if (token[0]) {
      wx.request({
        url: api.ShouCangApi,
        method:"POST",
        data:{
          id:id,
        },header:{
          'Authorization':'JWT '+token
        },
        success:res=>{
          // console.log(res)
          if(res.data.code==100){
           this.setData({
             "goodList.data.is_shoucang":res.data.is_shoucang
           })
            // this.data.goodList.is_shoucang = res.data.is_shoucang
            // model:value
            this.setData({
              "goodList.data.is_shoucang":res.data.is_shoucang
            })
            console.log()
            if(res.data.is_shoucang){ wx.showToast({
              title: '收藏成功',
              icon:'success'
            })}else{
              wx.showToast({
                title: '已取消收藏',
                icon:'success'
              })
            }   
          }else{
          }
        }
      })
    }else{
      wx.navigateTo({
        url: '../login/login',
      })
    }
  } catch (e) {
  
  }
 
  

 },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let goodId = options.IndexDetailId
    console.log(options,'>>>>')
    this.getDetail(goodId)
  },
})